package com.bxd.baodan.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.bxd.baodan.R;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.fragment.BaseFragment;
import com.bxd.baodan.fragment.QuestionListFragment;
import com.bxd.baodan.item.SortBean;
import com.bxd.baodan.item.SortItem;
import com.bxd.baodan.util.GlideUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.widget.FloatView;
import com.bxd.baodan.widget.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;


public class AllQuestionActivity extends BaseActivity {
    private ViewPager pager;
    private List<BaseFragment> list = new ArrayList<BaseFragment>();
    private FragmentPagerAdapter mAdapter;
    private PagerSlidingTabStrip tabs;
    private List<SortItem> sortlist = new ArrayList<>();
    private ImageView image;
    TextView tv_right;
    ImageView start_ask;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_all);
        setActionBarTitle("问答");
        tv_right = (TextView) findViewById(R.id.tv_bar_right);
        tv_right.setText("我的提问");


        image = (ImageView) findViewById(R.id.image);
        GlideUtil.loadImage(this, "http://s29.9956.cn/res/bxdapp/images/wenda.jpg", image);
        initView();
        FloatView.makeMenu(image, R.layout.layout_ask_icon).show();
        start_ask = (ImageView) findViewById(R.id.start_question);
        setClick();

    }

    private void setClick() {
        start_ask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(StartAskActivity.class);
            }
        });
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2017/11/6 我的问题
                if (loginStatus()) {
                    openActivity(MyQuestionActivity.class);
                }

            }
        });
    }

    private void initData(String result) {
        SortBean sortItem = new Gson().fromJson(result, SortBean.class);
        sortlist.clear();
        List<SortItem> netList = sortItem.data.list;
        sortlist.addAll(netList);

        list.clear();
        for (int i = 0; i < sortlist.size(); i++) {
            list.add(QuestionListFragment.newInstance(sortlist.get(i).id));
        }
        setTabsValue();
        mAdapter.notifyDataSetChanged();
    }


    private void initView() {
        // TODO: 2017/11/6 修改测试接口
        final NetUtil sortNet = new NetUtil(context, JsonApi.QUESTION_TAGS);

        sortNet.postRequest("加载中", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {

                initData(result);
            }

            @Override
            public void onError(String result) {

            }
        });


        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return sortlist.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return list.get(arg0);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return sortlist.get(position).name;
            }
        };

        pager = (ViewPager) findViewById(R.id.pager);
        tabs = (PagerSlidingTabStrip) findViewById(R.id.strip);
        pager.setAdapter(mAdapter);
        tabs.setViewPager(pager);


    }


    private void setTabsValue() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        // 设置Tab底部选中的指示器Indicator的高度
        tabs.setIndicatorHeight(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, dm));
        // 设置Tab底部选中的指示器 Indicator的颜色
        tabs.setIndicatorColorResource(R.color.green_lab);
        //设置指示器Indicatorin是否跟文本一样宽，默认false
        tabs.setIndicatorinFollowerTv(false);
        //设置小红点提示，item从0开始计算，true为显示，false为隐藏
        //        tabs.setMsgToast(2, true);
        //设置红点滑动到当前页面自动消失,默认为true
        tabs.setMsgToastPager(true);
        //设置Tab标题文字的颜色
        tabs.setTextColor(getResources().getColor(R.color.black));
        // 设置Tab标题文字的大小
        tabs.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, dm));
        // 设置选中的Tab文字的颜色
        tabs.setSelectedTextColorResource(R.color.green_lab);
        //设置Tab底部分割线的高度
        tabs.setUnderlineHeight(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.5f, dm));
        //设置Tab底部分割线的颜色
        tabs.setUnderlineColorResource(R.color.line_eb);
        // 设置点击某个Tab时的背景色,设置为0时取消背景色tabs.setTabBackground(0);
        //        tabs.setTabBackground(R.drawable.bg_tab);
        tabs.setTabBackground(0);
        // 设置Tab是自动填充满屏幕的
        tabs.setShouldExpand(true);

    }


}
